package com.bcu.calculus.service.serviceImpl;

import com.bcu.calculus.entity.Message;
import com.bcu.calculus.entity.Semester;
import com.bcu.calculus.entity.SemesterNumber;
import com.bcu.calculus.mapper.SemesterMapper;
import com.bcu.calculus.service.SemesterService;
import com.bcu.calculus.utils.SemesterUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@Service
public class SemesterServiceImpl implements SemesterService {

    @Resource
    private SemesterMapper semesterMapper;

    @Override
    public Message<?> insert(SemesterNumber semesterNumber) throws ParseException {
        Semester semester = SemesterUtil.change(semesterNumber);
        return semesterMapper.insert(semester) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> deleteByPrimaryKey(Integer semId) {
        return semesterMapper.deleteByPrimaryKey(semId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> updateByPrimaryKey(SemesterNumber semesterNumber) throws ParseException {
        Semester semester = SemesterUtil.change(semesterNumber);
        return semesterMapper.updateByPrimaryKeySelective(semester) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<Semester> selectByPrimaryKey(Integer semId) {
        return Message.success(null).add(semesterMapper.selectByPrimaryKey(semId));
    }

    @Override
    public Message<List<Semester>> selectAllSemester() {
        return Message.success(null).add(semesterMapper.selectAllSemester());
    }

    @Override
    public Message<Semester> findNowSemster() {
        return Message.success(null).add(semesterMapper.findNowSemester());
    }

    @Override
    public Message<Integer> findHaveNowSemester() {
        return Message.success("当前开放的学期个数").add(semesterMapper.findHaveOneNowSemester());

    }
}
